//
//  ChangeCell.m
//  AnnCar_user
//
//  Created by LiBenCan on 17/10/25.
//  Copyright © 2017年 LiBenCan. All rights reserved.
//

#import "ChangeCell.h"
#import "InputKeyBoardView.h"
#import "NumInputView.h"
#import "TextFiledView.h"
@interface ChangeCell()<UITextFieldDelegate>
@end
@implementation ChangeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = 0;
    }
    return self;
}

//创建UI
- (void)loadCellInputStr:(NSString *)inputStr PlaceHodelStr:(NSString *)hodelStr Title:(NSString *)titleStr;
{
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    
   
    //输入框
    _inputTV = [[TextFiledView alloc]initWithFrame:CGRectMake(0,0,SCREENW, 50*screenW_6) PlaceHodel:hodelStr Title:titleStr];
    _inputTV.backgroundColor = [UIColor whiteColor];
    _inputTV.inputTf.text = inputStr;
    _inputTV.inputTf.textColor = blueColor;
    [self.contentView addSubview:_inputTV];
    
    if (([titleStr isEqualToString:@"车牌号"] && [hodelStr isEqualToString:@"请输入车牌号"])) {
        _inputTV.inputTf.hidden = YES;
        [self addTF];
    }
    
    //电话图片
    if ([titleStr isEqualToString:@"师傅"] || [titleStr isEqualToString:@"洗车师傅"]) {
        float strWidthNumber = [TomamaUI getStringWeithtWith:inputStr andHeigth:SCREENW andFont:[UIFont systemFontOfSize:15*screenW_6]];
        _phoneImageV = [[UIImageView alloc]initWithFrame: CGRectMake(strWidthNumber+120*screenW_6, 16*screenW_6,18*screenW_6, 18*screenW_6)];
        _phoneImageV.image = [UIImage imageNamed:@"order_greentel"];
        [_inputTV addSubview:_phoneImageV];
    }
    
    
    //线条
     if ([titleStr isEqualToString:@"短信验证码"]) {
         
         [self.contentView addSubview:[ToolViewVC addCellLineLab:CGRectMake(SCREENW-131*screenW_6,15*screenW_6,1*screenW_6, 20*screenW_6)]];
         
         //发送按钮
         _sendBtn = [TomamaUI createButton:UIButtonTypeCustom Frame:CGRectMake(SCREENW-130*screenW_6,0,115*screenW_6,50*screenW_6) targ:self sel:@selector(sendSelect) titleColor:blueColor font:[UIFont systemFontOfSize:16*screenW_6] title:@"发送验证码" image:@"" backGroundImage:@""];
         [_sendBtn.layer setCornerRadius:5*screenW_6];
         [self.contentView addSubview:_sendBtn];
     }
}

-(void)addTF{
    
    for (int i = 0; i<7; i++) {
        float x = 100*screenW_6 +30*screenW_6*i;
        UITextField *TF = [[UITextField alloc]initWithFrame:CGRectMake(x, 10*screenW_6, 25*screenW_6, 30*screenW_6)];
        TF.delegate = self;
        TF.layer.borderWidth = 1*screenW_6;
        TF.tag = i + 100;
        TF.textAlignment = NSTextAlignmentCenter;
        TF.layer.borderColor = [TextGray CGColor];
        [self.contentView addSubview:TF];

        if (i==0) {
            [self addProvinceKeyBoard:TF];
        }else
        {
            [self addNumberKeyBoard:TF];
        }
    }
}

//车牌键盘
-(void)addProvinceKeyBoard:(UITextField *)tf{
    
    int verticalCount = 4;
    CGFloat kheight = SCREENW/10 + 8;
    InputKeyBoardView *keyBoardView = [[InputKeyBoardView alloc] initWithFrame:CGRectMake(0, SCREENH - kheight * verticalCount, SCREENW, kheight * verticalCount) withClickKeyBoard:^(NSString *character) {
        tf.text = [NSString stringWithFormat:@"%@%@", tf.text, character];
//        if (tf.text.length>1) {
//            tf.text = [tf.text substringWithRange:NSMakeRange(tf.text.length - 1,tf.text.length )];
//        }
        if (tf.tag < 106) {
            [tf resignFirstResponder];
            UITextField *nextTF = [self.contentView viewWithTag:tf.tag+1];
            [nextTF becomeFirstResponder];
        }
        
    } withDelete:^{
        if(tf.text.length > 0){
            tf.text = [tf.text substringWithRange:NSMakeRange(0, tf.text.length - 1)];
            if (tf.tag > 100) {
                [tf resignFirstResponder];
                UITextField *nextTF = [self.contentView viewWithTag:tf.tag -1];
                [nextTF becomeFirstResponder];
            }
        }
    }];
    tf.inputView = keyBoardView;
    
    
    
}
//车牌数字绘图
-(void)addNumberKeyBoard:(UITextField *)tf
{
    int verticalCount = 4;
    CGFloat kheight = SCREENW/10 + 8;
    
    NumInputView *numInputView = [[NumInputView alloc] initWithFrame:CGRectMake(0, SCREENH - kheight * verticalCount, SCREENW, kheight * verticalCount) withClickKeyBoard:^(NSString *character) {
        tf.text = [NSString stringWithFormat:@"%@%@", tf.text, character];
        if (tf.tag < 106) {
            [tf resignFirstResponder];
            UITextField *nextTF = [self.contentView viewWithTag:tf.tag+1];
            [nextTF becomeFirstResponder];
        }
//        if (tf.text.length>1) {
//            tf.text = [tf.text substringWithRange:NSMakeRange(tf.text.length - 1,tf.text.length )];
//        }
    } withDelete:^{
        if(tf.text.length > 0){
            tf.text = [tf.text substringWithRange:NSMakeRange(0, tf.text.length - 1)];
            if (tf.tag > 100) {
                [tf resignFirstResponder];
                UITextField *nextTF = [self.contentView viewWithTag:tf.tag -1];
                [nextTF becomeFirstResponder];
            }
        }
    }];
    numInputView.backgroundColor = [UIColor whiteColor];
    tf.inputView = numInputView;
}
-(void)sendSelect
{

}
@end
